#!/usr/bin/env python
#encoding:utf-8
#---------------------------------------
#Create：     2013-7-22
#Author：     longlimin
#Company：  kingsoft
#---------------------------------------

import ins

def get_fields(code):
    # return ({'datatype':xx, 'code':'xx'})
    sqlCmd = "select id from infoc_data_concern where code = '%s'" % code
    sqlRet = ins.global_conn.query(sqlCmd, 1)
    if sqlRet:
        sqlCmd = "select code,substring_index(datatype,'(',1) datatype from \
                 infoc_data_concern_field where data_concern_id=%s order by sequence" % sqlRet[0]["id"]
        sqlRet = ins.global_conn.query(sqlCmd, 1)
        res =  list(sqlRet)
        res.insert(0, {"code":"time", "datatype":"int"})
        res.fields.insert(1, {"code":"ip", "datatype":"string"})
        return res 
    return None

def data_points():
    sqlCmd = "select code from infoc_data_concern limit 1"
    sqlRet = ins.global_conn.query(sqlCmd)
    res = []
    for row in sqlRet:
        res.append(row[0])
    return res

def is_handle(code, dt, ip, logname):
    '''
        DDL =  CREATE TABLE `infoc_data_hive_import` (
              `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
              `code` varchar(255) NOT NULL,
              `dt` date DEFAULT NULL,
              `ip` varchar(32) DEFAULT NULL,
              `logname` varchar(255) DEFAULT NULL,
              `ctime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
              `rows` int(11) DEFAULT '0',
              `paser_time` double DEFAULT '0',
              `load_time` double DEFAULT '0',
              PRIMARY KEY (`id`),
              UNIQUE KEY `code` (`code`,`dt`,`ip`,`logname`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    '''
    sqlCmd = "select count(*) from infoc_data_hive_import where code='%s' and dt='%s'\
              and ip='%s' and logname='%s'" % (code, dt, ip, logname)
    sqlRet = ins.global_conn.query(sqlCmd)
    if sqlRet[0][0] == "0":
        return False
    return True
    
def add_handle(code, dt, ip, logname, rows, paser_time, load_time):
    sqlCmd = "insert ignore into infoc_data_hive_import(code, dt,ip, logname, rows, paser_time,\
         load_time) values('%s', '%s', '%s','%s', '%s', '%s','%s')" % (code, dt, ip, logname, rows, paser_time, load_time)
    effect_rows = ins.global_conn.execute(sqlCmd)
    return effect_rows
    
if __name__ == "__main__":
    print get_fields("duba_shouji_maindata")
    print data_points()
    print is_handle("","","","")